package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "I  am  your father"

	//1. strings.Fields 按照空格(可以有多个空格)切分字符串
	fields := strings.Fields(s)            //数组类型
	fmt.Println(fields)                    // [I am your father]
	fmt.Println(fields[1], len(fields[1])) //am 2

	//2. strings.Split 按照给定的正则表达式切分字符串，类似于 java中 .split函数
	splits := strings.Split(s, "a")
	for _, str := range splits {
		fmt.Println(str)
	}
	/*
		I
		m  your f
		ther
	*/

	//3. strings.Join 将一个字符串切片按照给定的字符串拼接
	strSlice := []string{"hello", "hahahah", "sdclone", "xyt"}
	joinedStr := strings.Join(strSlice, "_")
	fmt.Println(joinedStr) //hello_hahahah_sdclone_xyt

	//4. strings.TrimSpace 将首尾空格去除
	s2 := "   hello world   "
	s2Trim := strings.TrimSpace(s2)
	fmt.Println(s2Trim) //hello world

}
